package com.crm.CLdriving.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.crm.CLdriving.po.MailPO;
import com.crm.CLdriving.service.MailService;

import lombok.extern.log4j.Log4j2;

@Service
@Log4j2
public class MailServiceImpl implements MailService {

	@Autowired
    private JavaMailSenderImpl mailSender;
	
	@Override
	public MailPO sendMail(MailPO mailPO) {
		try {
            checkMail(mailPO);
            sendMimeMail(mailPO);
            return saveMail(mailPO);
        } catch (Exception e) {
            log.error("发送邮件失败:", e);
            mailPO.setStatus("fail");
            mailPO.setError(e.getMessage());
            return mailPO;
        }
	}
	
	private void checkMail(MailPO mailPO) {
        if (StringUtils.isEmpty(mailPO.getTo())) {
            throw new RuntimeException("邮件收信人不能为空");
        }
        if (StringUtils.isEmpty(mailPO.getSubject())) {
            throw new RuntimeException("邮件主题不能为空");
        }
        if (StringUtils.isEmpty(mailPO.getText())) {
            throw new RuntimeException("邮件内容不能为空");
        }
    }

    
    private void sendMimeMail(MailPO mailPO) {
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
            mailPO.setFrom(getMailSendFrom());
            messageHelper.setFrom(mailPO.getFrom());
            messageHelper.setTo(mailPO.getTo().split(","));
            messageHelper.setSubject(mailPO.getSubject());
            messageHelper.setText(mailPO.getText());
            if (!StringUtils.isEmpty(mailPO.getCc())) {
                messageHelper.setCc(mailPO.getCc().split(","));
            }
            if (!StringUtils.isEmpty(mailPO.getBcc())) {
                messageHelper.setCc(mailPO.getBcc().split(","));
            }
            if (mailPO.getMultipartFiles() != null) {
                for (MultipartFile multipartFile : mailPO.getMultipartFiles()) {
                    messageHelper.addAttachment(multipartFile.getOriginalFilename(), multipartFile);
                }
            }
            if (mailPO.getSentDate() == null) {
                mailPO.setSentDate(new Date());
                messageHelper.setSentDate(mailPO.getSentDate());
            }
            mailSender.send(messageHelper.getMimeMessage());
            mailPO.setStatus("ok");
            log.info("发送邮件成功：{}->{}", mailPO.getFrom(), mailPO.getTo());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    private MailPO saveMail(MailPO mailPO) {
        
        return mailPO;
    }

    
    public String getMailSendFrom() {
        return mailSender.getJavaMailProperties().getProperty("from");
    }


}
